import 'package:flutter/material.dart';
import 'package:management/model/gate_model.dart';
import 'package:management/model/open_gate_record_model.dart';
import 'package:management/model/user_model.dart';
import 'package:management/model/visitor_model.dart';
import 'package:sqflite/sqflite.dart';

class DBHelper {
  // String sqlCreateTableDeviceInfo =
  // "create table if not exits $tableDeviceInfo(sdkInt integer,) ";

  final String sqlCreateTableVisitor =
      """create table if not exists $tableVisitor(
      $columnName text not null,
      $columnGender text not null,
      $columnNationality text,
      $columnDateOfBirth text,
      $columnTypeOfCertificate text not null,
      $columnIDNumber text not null,
      $columnTelephoneNum text,
      $columnVisitorAddress text,
      $columnVisitorUnit text,
      $columnDepartmentInterviewed text,
      $columnInterviewee text,
      $columnVisitingMatters text,
      $columnRemarks text,
      $columnWhatToBring text,
      $columnLicensePlateNumber text,
      $columnNumberOfAccompanyingVisitors text,
      $columnRegistrationPoint text,
      $columnRegistrationDate text,
      $columnCheckInTime text);""";

  String sqlCreateTableGate = """create table if not exists $tableGate(
    $columnGateName text,
    $columnDepartmentId text,
    $columnDeviceId text,
    $columnGateMac text);
  """;

  String sqlCreateTableUser = """create table if not exists $tableUser(
    $columnUserName text,
    $columnUserPassWord text,
    $columnUserFaceToken text);
  """;

  String sqlCreateTableGateRecord =
      """create table if not exists $tableGateRecord(
      $columnGateRecordControlType integer,
      $columnGateName text,
      $columnGateRecordTime text,
      $columnGateRecordGateMac text,
      $columnGateRecordOperator text,
      $columnGateRecordPasserby text);
      """;

  ///

  Future init() async {
    WidgetsFlutterBinding.ensureInitialized();

    String? path;

    await openDatabase(
      path!,
      version: 1,
      onCreate: (db, version) async {
        var batch = db.batch();
        batch.execute(sqlCreateTableVisitor);
        batch.execute(sqlCreateTableUser);
        batch.execute(sqlCreateTableGateRecord);
        batch.execute(sqlCreateTableGate);
        batch.commit(continueOnError: true);
      },
    );
  }
}
